import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'
import { Layout} from '@/utils/routerHelper'
import { useI18n } from '@/hooks/web/useI18n'

const { t } = useI18n()

export const constantRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/redirect',
    component: Layout,
    name: 'Redirect',
    children: [
      {
        path: '/redirect/:path(.*)',
        name: 'Redirect',
        component: () => import('@/views/Redirect/Redirect.vue'),
        meta: {}
      }
    ],
    meta: {
      hidden: true,
      noTagsView: true
    }
  },
  {
    path: '/login',
    component: () => import('@/views/Login/index.vue'),
    name: 'Login',
    meta: {
      hidden: true,
      title: t('router.login'),
      noTagsView: true
    }
  },
  {
    path: '/404',
    component: () => import('@/views/Error/404.vue'),
    name: 'NoFind',
    meta: {
      hidden: true,
      title: '404',
      noTagsView: true
    }
  }
]

export const asyncRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/',
    component: Layout,
    name: '在线考试',
    meta: {
      title: t('在线考试'),
    },
    children: [
      {
        path: '/',
        name: '在线考试',
        component: () => import('@/views/examine/index.vue'),
        meta: {
          title: t('在线考试'),
          icon: 'ep:collection'
        }
      }
    ],
  },
  {
    path: '/result',
    component: Layout,
    redirect: '/result/index',
    name: '考试管理',
    meta: {
      title: t('考试管理'),
    },
    children: [
      {
        path: 'index',
        name: '考试管理',
        component: () => import('@/views/result/index.vue'),
        meta: {
          title: t('考试管理'),
          icon: 'ep:gold-medal'
        }
      },
      {
        path: 'result-info',
        name: '学生考试结果',
        component: () => import('@/views/result/info.vue'),
        meta: {
          title: t('学生考试结果'),
          hidden: true,
          noCache: true,
          noTagsView: true,
          canTo: true,
          activeMenu: '/result/result-info'
        }
      }
    ],
  },
  {
    path: '/account',
    component: Layout,
    redirect: '/account/index',
    name: '账号管理',
    meta: {
      title: t('账号管理'),
    },
    children: [
      {
        path: 'index',
        name: '账号管理',
        component: () => import('@/views/system-manage/user/index.vue'),
        meta: {
          title: t('账号管理'),
          icon: 'ep:user-filled',
        },
      },
      {
        path: 'account-insert',
        component: () => import('@/views/system-manage/user/add.vue'),
        name: '添加账号',
        meta: {
          title: t('添加账号'),
          hidden: true,
          noCache: true,
          noTagsView: true,
          canTo: true,
          activeMenu: '/account/account-insert'
        }
      }
    ],
  },
  {
    path: '/subject',
    name: '考题管理',
    redirect: '/subject/index',
    component: Layout,
    children: [
      {
        path: 'index',
        component: () => import('@/views/system-manage/subject/index.vue'),
        meta: {
          title: t('考题管理'),
          icon: 'ep:tickets',
        },
      }
    ]
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  strict: true,
  routes: constantRouterMap as RouteRecordRaw[],
  scrollBehavior: () => ({ left: 0, top: 0 })
})

export const resetRouter = (): void => {
  const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root']
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !resetWhiteNameList.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

export const setupRouter = (app: App<Element>) => {
  app.use(router)
}

export default router
